package com.dongnam.quanly.web;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.dongnam.quanly.web.util.LocaleMessages;

@ManagedBean(name = "JSFUtility")
@ApplicationScoped
public class JSFUtility {
	private static String[] spaceStrings = generateSpaceString();

	private static org.apache.log4j.Logger log = Logger.getLogger(JSFUtility.class);

	public JSFUtility() {

	}

	public static FacesMessage verifyPassword(String input, String expected) {
		if (input.equals(expected)) {
			return null;
		} else {
			FacesMessage message = LocaleMessages.getMessage("general_error_invalid");
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			return message;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> T findBean(String beanName) {
		FacesContext context = FacesContext.getCurrentInstance();
		return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
	}

	/**
	 * Assume bean name is the class name
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static <T> T findBean(Class beanClass) {
		FacesContext context = FacesContext.getCurrentInstance();
		return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanClass.getSimpleName() + "}",
				Object.class);
	}

	// Generate
	private static String[] generateSpaceString() {

		String[] spaceStrings = new String[20];
		String ch = "&nbsp;";
		spaceStrings[0] = ch;

		for (int i = 1; i < spaceStrings.length; i++) {
			spaceStrings[i] = spaceStrings[i - 1] + ch;
		}
		return spaceStrings;

	}

	public static String getParamValue(String paramName) {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String paramValue = params.get(paramName);
		log.debug("paramValue - Name : " + paramName + " Value: " + paramValue);

		return paramValue;
	}

	public static int getParamValueToInt(String paramName) {
		return Integer.parseInt(getParamValue(paramName));
	}

	// Severity = FacesMessage

	/**
	 * example : <br/>
	 * JSFUtility.setJSFFacesMessage(FacesMessage.SEVERITY_ERROR, "message" ,
	 * ""); <br/>
	 * <br/>
	 * example2 : <br/>
	 * JSFUtility.setJSFFacesMessage(FacesMessage.SEVERITY_INFO,
	 * "Save Successfully at " + JSFUtility.getCurrentDate(), "");
	 * 
	 * @param severity
	 * @param summary
	 * @param detail
	 * 
	 */
	public static void setJSFFacesMessage(Severity severity, String summary, String detail) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, summary, detail));
	}

	public static String getCurrentDate() {

		Date date = Calendar.getInstance().getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return sdf.format(date);
	}

	public static Timestamp getCurrentTimestamp() {
		Calendar calendar = Calendar.getInstance();
		java.util.Date now = calendar.getTime();

		// a java current time (now) instance
		java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
		return currentTimestamp;
	}

	public static java.sql.Date DateToSqlDate(Date inputDate) {
		return new java.sql.Date(inputDate.getTime());
	}

	public static String[] printSpace() {
		return spaceStrings;
	}
}
